﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TraceThisItem.Domain;

namespace TraceThisItem.WebUI.Items
{
	public partial class Details : System.Web.UI.Page
	{
		public int WorkFlowId { get; set; }
		public int CurrentStatusId { get; set; }
		public int ProjectId { get; set; }
		
		protected void Page_Load(object sender, EventArgs e)
		{
			this.SelectedItemDataSource.Select();
		}

		protected void SelectedItemDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void SelectedItemDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
		{
		
			lblSelectedItemSubject.Text = "Incidencia: " + ((Item)e.ReturnValue).Subject;
			lblSelectedItemDescription.Text = "Descripción: " + ((Item)e.ReturnValue).Description;
			lblSelectedItemProject.Text = "Proyecto: " + ((Item)e.ReturnValue).Project.Name;
			ProjectId = ((Item)e.ReturnValue).Project.Id;
			lblSelectedItemType.Text = "Tipo de incidencia: " + ((Item)e.ReturnValue).Type.Name;
			lblSelectedItemTeam.Text = "Equipo: " + ((Item)e.ReturnValue).Team ?? "Ninguno";
			ItemPriorityDropDownList.SelectedIndex = ((Item)e.ReturnValue).Priority;
			lblSelectedItemCreatedOn.Text = "Fecha de creación: " + ((Item)e.ReturnValue).CreatedOn.ToString();
			lblSelectedItemCurrentResponsible.Text = "Responsable:" + ((Item)e.ReturnValue).CurrentResponsible;
			lblSelectedItemCurrentStatus.Text = "Estado:" + ((Item)e.ReturnValue).CurrentStatus;
			WorkFlowId = ((Item)e.ReturnValue).Type.WorkflowId;
			CurrentStatusId = ((Item)e.ReturnValue).CurrentStatusId;
			WorkflowDataSource.Select();
			lblSelectedItemLastUpdateOn.Text = "Última actualización:" + ((Item)e.ReturnValue).LatestUpdate;
			lblSelectedItemCurrentPriority.Text = "Prioridad:" + ((Item)e.ReturnValue).CurrentPriority;
			TextBoxComments.Text = ((Item)e.ReturnValue).Comments;
		}

		protected void WorkflowDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void WorkflowDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
		{
			lblSelectedItemWorkflow.Text = "Workflow asociado:" + ((Workflow)e.ReturnValue).Name;
		}

		protected void WorkflowDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["workflowId"] = this.WorkFlowId;
		}

		protected void TraceabilityDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}


		protected void NextStatusesDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void NextStatusesDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["workflowId"] = this.WorkFlowId;
			e.InputParameters["currentStatusId"] = this.CurrentStatusId;
		}

		protected void NextStatusesDataSource_Selected(object sender, ObjectDataSourceStatusEventArgs e)
		{
			if (((IEnumerable<StatusNode>)e.ReturnValue).Count() == 0)
			{				
				NextStatusesDropDownList.Enabled = false;
				NextResponsibleDropDownList.Enabled = false;
				TextBoxComments.Enabled = false;
				ItemPriorityDropDownList.Enabled = false;
				this.btnModificarIncidencia.Enabled = false;
				lblActualizarDatos.Text = "La incidencia está finalizada, no puede actualizarse.";
			}
		}

		protected void ResponsableDataSource_ObjectCreating(object sender, ObjectDataSourceEventArgs e)
		{
			e.ObjectInstance = Facade.Instance;
		}

		protected void ResponsableDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters["projectId"] = this.ProjectId;
		}

		protected void btnModificarIncidencia_Click(object sender, EventArgs e)
		{
			if (Page.IsValid)
			{
				SelectedItemDataSource.Update();
			}
		}

		protected void SelectedItemDataSource_Updating(object sender, ObjectDataSourceMethodEventArgs e)
		{
			e.InputParameters["itemId"] = Request.QueryString["id"];
			e.InputParameters["comments"] = TextBoxComments.Text;
			e.InputParameters["responsible"] = Facade.Instance.GetUser(NextResponsibleDropDownList.SelectedValue);
			e.InputParameters["priority"] = ItemPriorityDropDownList.SelectedValue;
			e.InputParameters["nextStatus"] = Facade.Instance.GetStatusNodeById(Convert.ToInt32(NextStatusesDropDownList.SelectedValue));
		}

		protected void SelectedItemDataSource_Updated(object sender, ObjectDataSourceStatusEventArgs e)
		{
			Response.Redirect("~/Default.aspx",false);
		}

	
		

		


		
	

	}
}